package com.appkqxs.paser;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class KQXSParse {

	
	/**
	 * @param args
	 */
	public ArrayList<ArrayList<String>> getContent(String key,String url) throws Exception {
		ArrayList<String> listTemp = new ArrayList<String>();
		ArrayList<ArrayList<String>> lists = new ArrayList<>();
		//String url = "http://ketqua.net/xo-so-dien-toan-123.php";
		Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31").get();
		Elements els = doc.select("div.bo9.space");
		Elements element = els.get(0).select("table > tbody > tr");
		Elements el = element.select("td");
		
		if(key.equals("truyenthong")) {
			for(Element e : el) {
				int a = e.select("td.span-2.bol.f1b").size();
				if(a > 0) {
					listTemp = new ArrayList<String>();
					if(!e.select("td.span-2.bol.f1b").text().equals("")) {
						lists.add(listTemp);
					}
					continue;
				}
				listTemp.add(e.text());
				//System.out.println(e.text());
			}
		} else {
			for(Element e : el) {
				listTemp.add(e.text());
			}
			lists.add(listTemp);
		}
		
		return lists;
		
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			
			KQXSParse kqxsParse = new KQXSParse();
			int count = 0;
			
				for(ArrayList<String> ls : kqxsParse.getContent("truyenthong","http://ketqua.net/xo-so/kqauto/kqmb.html")) {
					System.out.println(" Giải "+count);
					for(String s : ls) {
						System.out.println(s);
					}
					
					count++;
				}
				count = 0;
				System.out.println("---------------------------"+new Date().getTime()+"----------------------------");
			
		
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

}
